﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Newtonsoft.Json;
using System.Data.SqlClient;

namespace PeisongWeb.admin
{
    public partial class buyerRange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rootCategoryBind();
                subCategoryBind();
                rptBind();

                Panel1.Title = string.Format("#{0}#采购范围", logic.company.getById(companyId).Company);
            }
        }

        public int companyId
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["company"]))
                {
                    return Convert.ToInt32(Request.QueryString["company"]);
                }
                return 0;
            }
            set
            {
                companyId = value;
            }
        }

        private void rootCategoryBind()
        {
            DataTable dt = logic.buyerRange.rootCategory_buyerRange_Subtraction(companyId);
            ddl_CategoryRoot.DataSource = dt;
            ddl_CategoryRoot.DataBind();
            ddl_CategoryRoot.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
        }

        private void subCategoryBind()
        {
            //当大类不等于空的时候，就绑定小类
            if (ddl_CategoryRoot.SelectedValue != "")
            {
                DataTable dt = logic.buyerRange.subCategory_buyerRange_Subtraction(companyId, Convert.ToInt32(ddl_CategoryRoot.SelectedValue));
                ddl_CategorySub.DataSource = dt;
                ddl_CategorySub.DataBind();
            }
            else
            {
                ddl_CategorySub.Items.Clear();
                ddl_CategorySub.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
        }

        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");

            if (!string.IsNullOrEmpty(ddl_CategorySub.SelectedValue))
                where.AppendFormat(" and categoryId={0}", ddl_CategorySub.SelectedValue);
            if (!string.IsNullOrEmpty(txt_ProductName.Text))
                where.AppendFormat(" and name like '%{0}%'", txt_ProductName.Text);
            if (!string.IsNullOrEmpty(txt_ProductNo.Text))
                where.AppendFormat(" and productNo='{0}'", txt_ProductNo.Text);

            DataSet ds = logic.buyerRange.listWithCompanySubtraction(companyId, where.ToString(), AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);

            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        // 搜索
        protected void lbn_search_Click(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void lbn_reset_Click(object sender, EventArgs e)
        {
        }

        protected void ddl_CategoryRoot_SelectedIndexChanged(object sender, EventArgs e)
        {
            subCategoryBind();
        }

        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "AddTo")
            {
                int productId = 0;
                string[] strpb = e.CommandArgument.ToString().Split(',');
                productId = Convert.ToInt32(strpb[0]);
                string businessnature = strpb[1];
                TextBox txt_remarks = (TextBox)e.Item.FindControl("txt_Remarks");
                TextBox txt_amount = (TextBox)e.Item.FindControl("txt_amount");
                double amount = Convert.ToDouble(txt_amount.Text.ToString());
                string remarks = txt_remarks.Text;
                logic.buyerRange.buyerRangeInsert(companyId, productId,remarks,amount,businessnature);
                
                Library.Script.ClientMsg("添加成功.");

                rptBind();
            }
        }

        // 添加选择项
        protected void btnDelSelected_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(hdSelectedProductIds.Value))
            {
                string[] allitems = hdSelectedProductIds.Value.Split(',');
                string[] noproductid = new string[allitems.Length];
                string[] productid = new string[allitems.Length];
                string[] remarks = new string[allitems.Length];
                string[] amounts = new string[allitems.Length];
                string[] amountsAndremarks = new string[allitems.Length];
                string[] businessnatures = new string[allitems.Length];
                for (int i = 0; i < allitems.Length; i++)
                {
                    string[] productidAndother = allitems[i].ToString().Split('|');
                    productid[i] = productidAndother[0];
                    noproductid[i] = productidAndother[1];
                    string[] businessnatureAndother = noproductid[i].ToString().Split('!');
                    businessnatures[i] = businessnatureAndother[0];
                    amountsAndremarks[i] = businessnatureAndother[1];
                    string[] amountsandremarks = amountsAndremarks[i].ToString().Split('*');
                    amounts[i] = amountsandremarks[0];
                    remarks[i] = amountsandremarks[1];
                }
                //string productId=null;
                //for (int i = 0; i < productid.Length; i++)
                //{
                //    if (productId == null)
                //        productId = productid[i];
                //    else {
                //        productId = productId + ",";
                //        productId = productId + productid[i];
                //    }
                //}
                //logic.buyerRange.buyerRangeSelectedItemsInsert(productId, companyId);
                for(int i=0;i<productid.Length;i++)
                {
                    logic.buyerRange.buyerRangeInsert(companyId, Convert.ToInt32(productid[i]), remarks[i], Convert.ToDouble(amounts[i]), businessnatures[i]);
                }
                rptBind();

                Library.Script.ClientMsg("添加成功.");
            }
        }

        // 按大类添加
        protected void btnRootCategoryDel_Click(object sender, EventArgs e)
        {
            int rootCategory = 0;
            int.TryParse(ddl_CategoryRoot.SelectedValue, out rootCategory);

            if (rootCategory != 0)
            {
                logic.buyerRange.buyerRangeInsertByRootCategory(companyId, rootCategory,logic.company.getById(companyId).BusinessNature);
                rptBind();

                Library.Script.ClientMsg("添加成功.");
            }
        }

        // 按小类添加
        protected void btnSubCategoryDel_Click(object sender, EventArgs e)
        {
            int subCategory = 0;
            int.TryParse(ddl_CategorySub.SelectedValue, out subCategory);

            if (subCategory != 0)
            {
                logic.buyerRange.buyerRangeInsertBySubCategory(companyId, subCategory, logic.company.getById(companyId).BusinessNature);
                rptBind();

                Library.Script.ClientMsg("添加成功.");
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("buyerRangeEdit2.aspx?company={0}", companyId));
        }

        // 按二级商品选用
        protected void btnSecondLevel_Command(object sender, CommandEventArgs e)
        {
            int rootCategory = 0;
            int.TryParse(ddl_CategoryRoot.SelectedValue, out rootCategory);

            int subCategory = 0;
            int.TryParse(ddl_CategorySub.SelectedValue, out subCategory);

            int categoryId = 0;
            int flag = 0;// 1 表示小类，2表示大类
            int.TryParse(e.CommandName,out flag);


            if (subCategory != 0)
            {
                categoryId = subCategory;
                flag = 1;
            }
            else
            {
                categoryId = rootCategory;
                flag = 0;
            }

            if (categoryId != 0)
            {
                logic.buyerRange.buyerRangeInsertBySecondLevel(companyId, categoryId,flag);
                rptBind();

                Library.Script.ClientMsg("添加成功.");
            }
        }

        //public string BusinessNature(string businessnature)
        //{
        //    if (businessnature == "1")
        //        return "工厂食堂级";
        //    else if (businessnature == "2")
        //        return "机关事业单位级";
        //    else if (businessnature == "3")
        //        return "院校级";
        //    else if (businessnature == "4")
        //        return "酒店餐饮级";
        //    else
        //        return "0";
 
        //}
    }
}
